from PySide6.QtCore import Qt
from PySide6.QtGui import QAction
from PySide6.QtGui import QIcon
from PySide6.QtGui import QKeySequence
from PySide6.QtGui import QShortcut
from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QMenu
from PySide6.QtWidgets import QSystemTrayIcon
from PySide6.QtWidgets import QWidget

import translate_api_youdao as translate_api
from ui_form import Ui_widget
# import translate_api

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = Ui_widget()
        self.ui.setupUi(self)
        # 信号槽
        self.ui.textEditChinese.textChanged.connect(self.chinese_to_english)
        self.ui.textEditEnglish.textChanged.connect(self.english_to_chinese)

        # 注册快捷键
        shortcut = QShortcut(QKeySequence('Alt+L'), self)
        shortcut.setContext(Qt.ApplicationShortcut)
        shortcut.activated.connect(self.toggle_widget)

    def toggle_widget(self):
        if self.isVisible():
            self.window_minimize()
            # self.hide()
        else:
            self.show()

    def window_minimize(self):
        if not self.isMinimized():
            self.setWindowState(self.windowState() | Qt.WindowMinimized)
            self.activateWindow()

    # 按Escape键退出
    def keyPressEvent(self, event):
        if event.key() == Qt.Key_Escape:
            self.window_minimize()
            # self.close()

    def chinese_to_english(self):
        # 暂时使用这样的方式来判断是否有回车换行
        content = self.ui.textEditChinese.toPlainText()
        if not content or content[-1] not in  ('\r', '\n'):
            return
        content = content.strip()
        content = '  '.join(content.splitlines())
        # print(f'翻译中文: {content} ...')
        result = translate_api.chinese_to_english(content)
        self.ui.textEditEnglish.setMarkdown(result)

    def english_to_chinese(self):
        content = self.ui.textEditEnglish.toPlainText()
        if not content or content[-1] not in  ('\r', '\n'):
            return
        content = content.strip()
        content = '  '.join(content.splitlines())
        # print(f'翻译英语: {content} ...')
        result = translate_api.english_to_chinese(content)
        self.ui.textEditChinese.setMarkdown(result)


if __name__ == '__main__':
    app = QApplication()
    # 打二进制包后找不到路径
    # with open('./style.qss') as f:
    #     style = f.read()
    style = """\
QLabel {
    font-family: "Helvetica Neue", "Yuanti SC";
    font: 12px;
    color: #63635f;
}

QTextEdit {
    font-family: "Helvetica Neue", "Hei";
    font: 16px;
    color: #44494b;
    border-radius: 0px;
    border: 1px solid rgb(200, 200, 200);
    background-color: white;
}

QTextEdit:focus {
    border-bottom: 2px solid rgb(22, 117, 189);
}
"""
    app.setStyleSheet(style)
    widget = Widget()
    widget.show()

    # 创建系统托盘
    app.setQuitOnLastWindowClosed(False)
    tray = QSystemTrayIcon()
    tray.setIcon(QIcon('logo.png'))
    tray.setVisible(True)
    tray.activated.connect(widget.show)

    # 系统托盘右键菜单
    menu = QMenu()
    close_action = QAction('关闭')
    close_action.triggered.connect(app.quit)
    menu.addAction(close_action)
    tray.setContextMenu(menu)

    tray.show()
    app.exec()
